#include <resource.h>

int callResourceUser(u32int rId, u32int fId, int p1, int p2, int p3, int p4)
{
  // Perform a syscall.
  int ret;
  asm volatile("int $50" : "=a" (ret) : "0" (rId), "b" (fId), "c" (p1),
               "d" (p2), "S" (p3), "D" (p4));
  return ret;
}

u32int requestResource(const char *name)
{
  // Perform a syscall.
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_REQUEST), "b" ((int)name));
  return (u32int)ret;
}

int callResource(u32int rId, const int fId)
{
  return callResourceUser(rId, (u32int)fId);
}

int createResource(Callable *target, const char *name)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_CREATERESOURCE), "b" ((int)name), "c" ((int)target));
  return ret;
}

int addFunction(int resource, Resource::Call0 pCall, int fId)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_ADDFUNC0), "b" (resource), "c" (&pCall), "d" (fId));
  return ret;
}

int addFunction(int resource, Resource::Call1 pCall, int fId)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_ADDFUNC1), "b" (resource), "c" (&pCall), "d" (fId));
  return ret;
}

int addFunction(int resource, Resource::Call2 pCall, int fId)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_ADDFUNC2), "b" (resource), "c" (&pCall), "d" (fId));
  return ret;
}

int addFunction(int resource, Resource::Call3 pCall, int fId)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_ADDFUNC3), "b" (resource), "c" (&pCall), "d" (fId));
  return ret;
}

int addFunction(int resource, Resource::Call4 pCall, int fId)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_ADDFUNC4), "b" (resource), "c" (&pCall), "d" (fId));
  return ret;
}

int addFunction(int resource, Resource::Call5 pCall, int fId)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_ADDFUNC5), "b" (resource), "c" (&pCall), "d" (fId));
  return ret;
}

int destroyResource(int resource)
{
  int ret;
  asm volatile("int $30" : "=a" (ret) : "0" (SYSCALL_DESTROYRESOURCE), "b" (resource));
  return ret;
}
